package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootLottery;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootLotterySearchForm;
import org.footballmanager.pandafootball.system.mapper.FootLotteryMapper;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootLotteryService;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.FootZjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FootLotteryServiceImpl extends ServiceImpl<FootLottery> implements FootLotteryService {
	/*白色	75以下	15分钟	10PB
	    绿色	75-79	30分钟	30PB
	    蓝色	80-83	2小时	50PB
	    紫色	84-89	6小时	80PB
	    橙色	90-100	24小时	100PB */
	//都换算成秒
	private static final long WHITE = 300;
	private static final long GREEN = 1800;
	private static final long BLUE = 7200;
	private static final long PERPLE = 21300;
	private static final long ORANGE = 86400;

	private static final double WHITEPRICE = 10D;
	private static final double GREENPRICE = 30D;
	private static final double BLUEPRICE = 50D;
	private static final double PURPLEPRICE = 80D;
	private static final double ORANGEPRICE = 100D;
	
	@Autowired
	private FootLotteryMapper footLotteryMapper;
	@Autowired
	private FootChipService footChipService;
	@Autowired
	private FootTeamService footTeamService;
	@Autowired
	private FootZjService footZjService;

	@Override
	public String reComputeWhiteDate(Long userId, boolean isPay, HttpServletRequest request) {
		return processRandomChip(userId, isPay, request, WHITEPRICE, WHITE, 1, "白色");
	}

	@Override
	public String reComputeGreenDate(Long userId, boolean isPay, HttpServletRequest request) {
		return processRandomChip(userId, isPay, request, GREENPRICE, GREEN, 2, "绿色");
	}

	@Override
	public String reComputeBlueDate(Long userId, boolean isPay, HttpServletRequest request) {
		return processRandomChip(userId, isPay, request, BLUEPRICE, BLUE, 3, "蓝色");
	}

	@Override
	public String reComputePurpleDate(Long userId, boolean isPay, HttpServletRequest request) {
		return processRandomChip(userId, isPay, request, PURPLEPRICE, PERPLE, 4, "紫色");
	}

	@Override
	public String reComputeOrangeDate(Long userId, boolean isPay, HttpServletRequest request) {
		return processRandomChip(userId, isPay, request, ORANGEPRICE, ORANGE, 5, "橙色");
	}

	private String processRandomChip(Long userId, boolean isPay, HttpServletRequest request, Double price,
									 Long initPrice, Integer type, String color){
		if(isPay){
			FootTeam team = footTeamService.getTeamByUserid(userId);
			if(team != null && team.getQdZzj() > price){
				team.setQdZzj(team.getQdZzj() - price);
				footTeamService.updateNotNull(team, request);
				footZjService.sendZj(userId, price, false, "因购买" + color + "碎片花费：€" +
						new DecimalFormat(",###").format(price) +
						",当前的球队总资金为:€" +
						new DecimalFormat(",###").format(team.getQdZzj()), request);
			}else{
				return "nomoney";
			}
		}
		FootLottery lottery = findLotteryByUserId(userId);
		if(null != lottery){
			Date date = new Date(new Date().getTime() + initPrice * 1000);
			if(type.equals(1)){
				lottery.setLotteryDateWhite(date);
			}else if(type.equals(2)){
				lottery.setLotteryDateGreen(date);
			}else if(type.equals(3)){
				lottery.setLotteryDateBlue(date);
			}else if(type.equals(4)){
				lottery.setLotteryDatePurple(date);
			}else{
				lottery.setLotteryDateOrange(date);
			}
			this.updateNotNull(lottery, request);
		}
		footChipService.randomGetChip(userId, type, 1, request);
		return "ok";
	}

	@Override
	public FootLottery findLotteryByUserId(Long userId) {
		Example example = new Example(FootLottery.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(Constants.USERID, userId);
		return this.findByExample(example);
	}

	@Override
	public void saveNewLottery(FootUser user, HttpServletRequest request) {
		FootLottery lo = new FootLottery();
		Date date = new Date();
		lo.setUserId(user.getId());
		lo.setLotteryDateWhite(date);
		lo.setLotteryDateGreen(date);
		lo.setLotteryDateBlue(date);
		lo.setLotteryDatePurple(date);
		lo.setLotteryDateOrange(date);
		this.save(lo, request);
	}

    /**
     * 翻页方法
     *
     * @param footLotterySearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootLottery> findall(FootLotterySearchForm footLotterySearchForm) {
        return footLotteryMapper.findall(footLotterySearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootLottery get(Serializable id) {
		return footLotteryMapper.findById(id);
	}

    /**
     * 查询
     *
     * @param entityname            实体类名
     * @param map                   参数
     * @param footLotterySearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootLotterySearchForm footLotterySearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footLotterySearchForm);
        Page<FootLottery> page = PageHelper.startPage(footLotterySearchForm.getPageIndex(), footLotterySearchForm.getPageSize());
        List<FootLottery> list = this.findall(footLotterySearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }
}
